package guoanmaker.operator.business.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.OperatorComplaintType;
import guoanmaker.operator.business.model.OperatorDepartment;
import guoanmaker.operator.business.model.OperatorQuestionOrder;
import guoanmaker.operator.business.service.OperatorQuestionOrderServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

/**
 * 工单模块
 * 
 * @ClassName: OperatorQuestionOrderController
 * @Description:TODO (这里用一句话描述这个类的作用) 
 * @author lihongyu
 * @date 2017年8月15日 下午6:22:04    
 */
@RestController
@RequestMapping(ApiController.OPERATOR_QUESTIONORDER)
public class OperatorQuestionOrderController {
	@Autowired
	private OperatorQuestionOrderServiceInterface operatorQuestionOrderServiceInterface;

	@ApiOperation(value = "保存工单", notes = "新增工单")
	@PostMapping(value = "saveQuestionOrder")
	public AjaxJson saveQuestionOrder(@RequestBody OperatorQuestionOrder operatorQuestionOrder,
			HttpServletRequest request) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			OperatorQuestionOrder questionOrder = operatorQuestionOrderServiceInterface
					.saveOperatorQuestionOrder(operatorQuestionOrder);
			ajaxJson.setSuccess(true).setStatus(200).setData(questionOrder);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("插入失败");
		}
		return ajaxJson;
	}

	/**
	 * 运营人员查询
	 * 
	 * @Title: getAllQuestionOrder 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param page
	 * @param size
	 * 			@return    设定文件 
	 * @return AjaxJson    返回类型  @throws 
	 */
	@ApiOperation(value = "查找所有工单", notes = "查找所有工单")
	@RequestMapping(value = "/getAllQuestionOrder", method = RequestMethod.POST)

	public AjaxJson getAllQuestionOrder(@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			Page<OperatorQuestionOrder> oqos = operatorQuestionOrderServiceInterface
					.getOperatorQuestionOrderBytime(page, size);
			if (oqos.getContent().size() != 0) {
				ajaxJson.setSuccess(true).setStatus(200).setData(oqos);
			} else {
				ajaxJson.setSuccess(true).setStatus(250).setData("没有查到数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(200).setData("查询失败");
		}
		return ajaxJson;
	}

	/**
	 * 运营人员查询根据订单状态查询 1是待处理 2处理中工单 3待确认关闭订单 4已关闭订单
	 * 
	 * @Title: getAllQuestionOrderBystatus 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param page
	 * @param size
	 * @param orderStatus
	 * 			@return    设定文件 
	 * @return AjaxJson    返回类型  @throws 
	 */
	@ApiOperation(value = "根据工单状态查找工单列表", notes = "根据工单状态查找工单列表")
	@RequestMapping(value = "/getAllQuestionOrderBystatus", method = RequestMethod.POST)
	public AjaxJson getAllQuestionOrderBystatus(@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size, String orderStatus) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			Page<OperatorQuestionOrder> oqos = operatorQuestionOrderServiceInterface
					.getOperatorQuestionOrderByorderStatus(page, size, orderStatus);
			if (oqos.getContent().size() != 0) {
				ajaxJson.setSuccess(true).setStatus(200).setData(oqos);
			} else {
				ajaxJson.setSuccess(true).setStatus(250).setData("没有查到数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(200).setData("查询失败");
		}
		return ajaxJson;
	}

	/**
	 * 部门工单查询
	 * 
	 * @Title: getAllQuestionOrderBydepartmentId 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param page
	 * @param size
	 * @param departmentId
	 * 			@return    设定文件 
	 * @return AjaxJson    返回类型  @throws 
	 */
	@ApiOperation(value = "根据部门查找工单列表", notes = "根据部门查找工单列表")
	@RequestMapping(value = "/getAllQuestionOrderBydepartmentId", method = RequestMethod.POST)
	public AjaxJson getAllQuestionOrderBydepartmentId(@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size, String departmentId) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			Page<OperatorQuestionOrder> oqos = operatorQuestionOrderServiceInterface
					.getOperatorQuestionOrderBytimeAnddepartmentId(page, size, departmentId);
			if (oqos.getContent().size() != 0) {
				ajaxJson.setSuccess(true).setStatus(200).setData(oqos);
			} else {
				ajaxJson.setSuccess(true).setStatus(250).setData("没有查到数据");
			}
		} catch (Exception e) {
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
		return ajaxJson;
	}

	/**
	 * 部门工单（分工单状态）
	 * 
	 * @Title: getAllQuestionOrderBystatusAndDepartment 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param page
	 * @param size
	 * @param orderStatus
	 * @param departmentId
	 * 			@return    设定文件 
	 * @return AjaxJson    返回类型  @throws 
	 */
	@ApiOperation(value = "根据工单状态查找工单列表", notes = "根据工单状态查找工单列表")
	@RequestMapping(value = "/getAllQuestionOrderBystatusAndDepartment", method = RequestMethod.POST)
	public AjaxJson getAllQuestionOrderBystatusAndDepartment(
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size, String orderStatus, String departmentId) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			Page<OperatorQuestionOrder> oqos = operatorQuestionOrderServiceInterface
					.getOperatorQuestionOrderBytimeAnddepartmentIdAndStatus(page, size, departmentId, orderStatus);
			if (oqos.getContent().size() != 0) {
				ajaxJson.setSuccess(true).setStatus(200).setData(oqos);
			} else {
				ajaxJson.setSuccess(true).setStatus(250).setData("没有查到数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
		return ajaxJson;
	}

	/**
	 * 接单，转单操作
	 * 
	 * @Title: updateQuestionOrder 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param operatorQuestionOrder
	 * @param request
	 * 			@return    设定文件 
	 * @return AjaxJson    返回类型  @throws 
	 */
	@ApiOperation(value = "更新工单", notes = "更新工单更改状态")
	@PostMapping(value = "updateQuestionOrder")
	public AjaxJson updateQuestionOrder(Integer stauts,String userid,String id,String orderStatus,String departmentId,String disposeOpinion,String disposeWay,
			String refund,Double money) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			int a=operatorQuestionOrderServiceInterface
					.updateOperatorQuestionOrder(stauts,userid,id,orderStatus,departmentId,disposeOpinion,disposeWay,refund,money);
			ajaxJson.setSuccess(true).setStatus(200).setData("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("更新失败");
		}
		return ajaxJson;
	}

	@ApiOperation(value = "根据id查找工单详情", notes = "根据id查找工单详情")
	@RequestMapping(value = "/getQuestionOrderByid", method = RequestMethod.POST)
	public AjaxJson getQuestionOrderByid(String id) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			Map<String,Object> map = operatorQuestionOrderServiceInterface.selectOperatorQuestionOrderById(id);
			if (map.size() != 0) {
				ajaxJson.setSuccess(true).setStatus(200).setData(map);
			} else {
				ajaxJson.setSuccess(true).setStatus(250).setData("没有数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询错误");
		}
		return ajaxJson;
	}

	@ApiOperation(value = "查询所有的投诉原因列表", notes = "查询所有的投诉原因列表")
	@RequestMapping(value = "/getComplaintType", method = RequestMethod.POST)
	public AjaxJson getComplaintType() {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			List<OperatorComplaintType> list = operatorQuestionOrderServiceInterface.getComplaintType();
			if (list.size() != 0) {
				ajaxJson.setSuccess(true).setStatus(200).setData(list);
			} else {
				ajaxJson.setSuccess(true).setStatus(250).setData("无数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
		return ajaxJson;
	}
	@ApiOperation(value = "查询使用中的部门列表", notes = "查询使用中的部门列表")
	@RequestMapping(value = "/findByStauts", method = RequestMethod.POST)
	public AjaxJson findByStauts() {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			List<OperatorDepartment> list = operatorQuestionOrderServiceInterface.findByStauts();
			if (list.size() != 0) {
				ajaxJson.setSuccess(true).setStatus(200).setData(list);
			} else {
				ajaxJson.setSuccess(true).setStatus(250).setData("无数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
		return ajaxJson;
	}

}
